package com.moba.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.moba.domain.BaseClass;
import com.moba.domain.BaseData;
import com.moba.domain.SysSetting;
import com.moba.emums.YesOrNo;
import com.moba.service.BaseClassService;
import com.moba.service.BaseDataService;
import com.moba.service.SysSettingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author liujia
 * @desc 基础的类
 */
@Component
public class BaseController {

  @Resource BaseDataService baseDataService;

  @Resource BaseClassService baseClassService;
  @Resource SysSettingService sysSettingService;

  public static DecimalFormat dfFormat = new DecimalFormat("0.00");

  public static DecimalFormat dfFormat0 = new DecimalFormat("0");

  public static SimpleDateFormat sdFormatYm = new SimpleDateFormat("yyyy-MM");

  public static SimpleDateFormat sdFormatYmd = new SimpleDateFormat("yyyy-MM-dd");

  public static SimpleDateFormat sdFormatYmdhms = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

  protected static final Logger logger = LoggerFactory.getLogger(BaseController.class);

  public String getCtxPath(HttpServletRequest request) {
    return this.getCtxPath(request, true);
  }

  public String getCtxPath(HttpServletRequest request, boolean is_add_port) {

    StringBuffer ctx = new StringBuffer();
    ctx.append(request.getScheme()).append("://").append(request.getServerName());
    if (is_add_port) {
      ctx.append(":").append(request.getServerPort());
    }
    ctx.append(request.getContextPath());

    return ctx.toString();
  }

  /** 枚举转换成为json */
  public JSONArray parseEnmuToJSONArray(Class<? extends Enum> enumClass, Enum<?>[] obj) {
    SerializeConfig config = new SerializeConfig();
    config.configEnumAsJavaBean(enumClass);
    return JSONArray.parseArray(
        JSON.toJSONString(obj, config, SerializerFeature.WriteMapNullValue));
  }

  /** 枚举list转换成为json */
  public JSONArray parseEnmuListToJSONArray(Class enumClass, List<?> obj) {
    SerializeConfig config = new SerializeConfig();
    config.configEnumAsJavaBean(enumClass);
    return JSONArray.parseArray(
        JSON.toJSONString(obj, config, SerializerFeature.WriteMapNullValue));
  }

  /** 通过基础数据类型，查找对应的基础数据类型的集合 */
  public List<BaseData> getBaseDataList(Integer type) {
    BaseData baseData = new BaseData();
    baseData.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    baseData.setType(type);
    return this.baseDataService.getEntityList(baseData);
  }

  /** 通过基础数据类型，查找对应的基础数据 */
  public BaseData getBaseData(Integer type) {
    BaseData baseData = new BaseData();
    baseData.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    baseData.setType(type);
    return this.baseDataService.getEntity(baseData);
  }

  /** 通过基础数据id，查找对应的基础数据 */
  public BaseData getBaseDataById(Integer id) {
    BaseData baseData = new BaseData();
    baseData.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    baseData.setId(id);
    return this.baseDataService.getEntity(baseData);
  }

  /** 通过基础类别，查找对应的基础类别数据集合 */
  public List<BaseClass> getBaseClassesList(Integer clsScope) {
    BaseClass baseClass = new BaseClass();
    baseClass.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    baseClass.setClsScope(clsScope);
    List<BaseClass> list = this.baseClassService.getEntityList(baseClass);
    return list;
  }

  public JSONArray getBaseClassesListChoose(Integer clsScope) {
    BaseClass baseClass = new BaseClass();
    baseClass.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    baseClass.setClsScope(clsScope);
    List<BaseClass> list = this.baseClassService.getEntityList(baseClass);
    JSONArray oneList = new JSONArray();
    JSONObject obj = null;
    for (BaseClass cur : list) {
      if (cur.getClsLevel().intValue() == 1) {
        obj = new JSONObject();
        obj.put("label", cur.getClsName());
        obj.put("value", cur.getClsId());
        oneList.add(obj);
      }
    }
    for (Object item : oneList) {
      JSONObject itemObj = (JSONObject) item;
      System.out.println(itemObj.get("label"));
      JSONArray childrenList = new JSONArray();

      for (BaseClass cur : list) {
        if (cur.getParId().intValue() == (Integer) itemObj.get("value")) {
          JSONObject children = new JSONObject();
          children.put("label", cur.getClsName());
          children.put("value", cur.getClsId());
          childrenList.add(children);
        }
      }
      itemObj.put("children", childrenList);
    }
    return oneList;
  }

  /** 通过基础数据类型，查找对应的基础数据类型的集合 */
  public BaseClass getBaseClass(Integer clsId) {
    BaseClass baseClass = new BaseClass();
    baseClass.setClsId(clsId);
    baseClass.setIsDel(YesOrNo.YES_OR_NO_0.getIndex());
    return this.baseClassService.getEntity(baseClass);
  }

  /** 登录密码验证 */
  public static boolean checkPassword(String password) {
    Pattern Password_Pattern = Pattern.compile("^(?=.*[0-9])(?=.*[a-zA-Z])(.{8,20})$");
    Matcher matcher = Password_Pattern.matcher(password);
    if (matcher.matches()) {
      return true;
    }
    return false;
  }

  /** 安全密码验证 */
  public static boolean checkSafePassword(String password) {
    Pattern Password_Pattern = Pattern.compile("^(?=.*[0-9])(.{6})$");
    Matcher matcher = Password_Pattern.matcher(password);
    if (matcher.matches()) {
      return true;
    }
    return false;
  }

  public SysSetting getSysSetting(String str) {
    SysSetting sys = new SysSetting();
    sys.setMemo(str);
    sys = sysSettingService.getEntity(sys);
    return sys;
  }

  public int differentDaysByMillisecond(Date date1, Date date2) {
    int days = (int) ((date2.getTime() - date1.getTime()) / (1000 * 3600 * 24));
    return days;
  }
}
